#include "InputHandler.h"

// private constructor - singleton
InputHandler::InputHandler() 
{
}

InputHandler::~InputHandler() 
{
}

InputHandler* InputHandler::m_xInstance = nullptr;

InputHandler* InputHandler::GetInstance()
{

	if (m_xInstance == nullptr)
	{
		m_xInstance = new InputHandler;
	}
	return m_xInstance;
}

void InputHandler::Destroy()
{
	if (m_xInstance != NULL)
	{
		delete m_xInstance;
		m_xInstance = NULL;
	}
}

void InputHandler::Initialize()
{
	for (unsigned int i = 0; i < sf::Mouse::ButtonCount; i++)
	{
		m_bMBCurrentState.push_back(false);
		m_bMBPrevState.push_back(false);
	}

	for (unsigned int i = 0; i < sf::Keyboard::KeyCount; i++)
	{
		m_bKeys.push_back(false);
		m_bPrevKeys.push_back(false);
	}
}

bool InputHandler::isPressed(sf::Keyboard::Key key)
{
	if (sf::Keyboard::isKeyPressed(key))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool InputHandler::isMouseDownFirst(sf::Mouse::Button MB)
{
	if (m_bMBCurrentState[MB] == true && m_bMBPrevState[MB] == false)
	{
		return true;
	}
	return false;
}

bool InputHandler::isMouseUpFirst(sf::Mouse::Button MB)
{
	if ( m_bMBCurrentState[MB] == false && m_bMBPrevState[MB] == true )
	{
		return true;
	} 
	return false;
}

bool InputHandler::isKeyPressedFirst(sf::Keyboard::Key key)
{
	if (m_bKeys[key] == true && m_bPrevKeys[key] == false)
	{
		return true;
	}
		 
	return false;
}

bool InputHandler::isKeyReleased(sf::Keyboard::Key key)
{
	if (m_bKeys[key] == false && m_bPrevKeys[key] == true)
		return true;

	return false;
}

bool InputHandler::isPressed(sf::Mouse::Button mouse)
{
	return sf::Mouse::isButtonPressed(mouse);
}

void InputHandler::Update()
{
	m_bMBPrevState = m_bMBCurrentState;
	m_bPrevKeys = m_bKeys;
	m_bMBCurrentState.clear();
	m_bKeys.clear();
	for (unsigned int i = 0; i < sf::Mouse::ButtonCount; i++)
	{
		bool buttonstate = sf::Mouse::isButtonPressed((sf::Mouse::Button)i);
		m_bMBCurrentState.push_back(buttonstate);
	}

	for (unsigned int i = 0; i < sf::Keyboard::KeyCount; i++)
	{
		bool key = sf::Keyboard::isKeyPressed((sf::Keyboard::Key)i);
		m_bKeys.push_back(key);
	}

}

sf::Mouse::Button InputHandler::MouseButtons(int i)
{
	switch (i)
	{
	case 0:
		return sf::Mouse::Left;
		break;
	case 1:
		return sf::Mouse::Right;
		break;
	case 2:
		return sf::Mouse::Middle;
		break;
	case 3:
		return sf::Mouse::XButton1;
		break;
	case 4:
		return sf::Mouse::XButton2;
		break;
	default:
		return sf::Mouse::ButtonCount;
		break;
	}
}

std::vector<bool> InputHandler::getAllCurrentMousePresses()
{

	return m_bMBCurrentState;
}

std::vector<bool> InputHandler::getAllPreviousMousePresses()
{
	return m_bMBPrevState;
}

sf::Vector2i InputHandler::GetMousePosition()
{
	return sf::Mouse::getPosition(*DisplayManager::GetInstance()->GetRenderWindow());
}